import AI from "./AI";

export default class AIManager extends Laya.Script {

    // 更多参数说明请访问: https://ldc2.layabox.com/doc/?nav=zh-as-2-4-0
    AIPrefabArr=[]
    spawnAIArr=[]
    shengcheng=true;
    constructor() { super(); }
    onAwake(){
    this.loadFoodPrefab();
    Laya.stage.on("Home1",this,function(){this.shengcheng=false})
    }
    loadFoodPrefab(){
    var pathArr=[
    "prefab/AI.json",
    ]
    var infoArr=[];
    for(var i=0;i<pathArr.length;i++){
    infoArr.push({url:pathArr[i],type:Laya.Loader.PREFAB})
    }
    Laya.loader.load(infoArr,Laya.Handler.create(this,function(result){
    for(var i=0;i<pathArr.length;i++){
    this.AIPrefabArr.push(Laya.loader.getRes(pathArr[i]))
    }
    this.ranTime=this.getRandom(300,800);
    Laya.timer.loop(this.ranTime,this,function(){
    this.spawn();
    })
    }))
    }
    spawn(){
        if(!this.shengcheng)return;
    var arrX=[1138,849,1750]
    var arrY=[361,617,747]
    var y=arrY[this.getRandom(0,arrY.length-1)];
    var x=arrX[this.getRandom(0,arrY.length-1)];
    var AIIndex=this.getRandom(0,this.AIPrefabArr.length-1);
    var ai=Laya.Pool.getItemByCreateFun(AIIndex.toString(),function(){
    return this.AIPrefabArr[AIIndex].create()},this);
    Laya.stage.getChildAt(0).getChildAt(0).addChild(ai);
    ai.pos(x,y);
    ai.getComponent(AI).Init(AIIndex.toString());
    this.spawnAIArr.push(ai);
    
    }
    getRandom(min,max){
    var value=Math.random()*(max-min);
    value=Math.round(value);
    return min+value;
    }
    }
